package com.mostromas.ess.util.tools;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 */
public class DateUtil {
    public static String getOrderNum() {
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        return df.format(date);
    }

    // 获取日期，格式：yyyy-MM-dd HH:mm:ss
    public static String getDateFormatter() {
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(date);
    }
    /**
     * 获取日期，格式：yyyyMMdd
     * @return String yyyyMMdd
     */
    public static String getDate() {
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        return df.format(date);
    }

    /**
     * 获取指定时间的前多少天
     * @param date  指定的时间
     * @param time  指定的天数
     * @return
     */
    public static Date getNextDay(Date date,Integer time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, +time);
        date = calendar.getTime();
        calendar.clear();
        return date;
    }

    /**
     * 获取指定时间的后多少天
     * @param date  指定的时间
     * @param time  指定的天数
     * @return
     */
    public static Date getBackDay(Date date,Integer time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -time);
        date = calendar.getTime();
        calendar.clear();
        return date;
    }
    /**
     * 获取23:59:59时间
     * @param i 0为今天 1为明天 -1为昨天
     * @return
     */
    public static Date getDateByZero(Integer i){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DAY_OF_MONTH, i);
        return cal.getTime();
    }
    /**
     * 获取当前时间推迟五分钟
     * @param date
     * @return
     */
    public static Date getPostponeMinute(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, +5);
        date = calendar.getTime();
        calendar.clear();
        return date;
    }
    /**
     * 比较时间
     * @param current 当前时间
     * @param past	已有的时间
     * @return 如果当前时间小于已有时间，代表还在有效期，返回true
     */
    public static boolean ompareDate(Date current,Date past){
        boolean flase = false;
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(current);
        c2.setTime(past);
        int result=c1.compareTo(c2);
        if(result<0){
            flase = true;
        }
        return flase;
    }
    /**
     获取精确到毫秒的时间戳
     * @param date
     * @return
     **/
    public static Long getTimestamp(Date date){
        if (null == date) {
            return (long) 0;
        }
        String timestamp = String.valueOf(date.getTime());
        return Long.valueOf(timestamp);
    }
}
